<?php

namespace App\Listeners\LikeCreated;

use App\Events\LikeCreated;
use App\Models\Comment;
use App\Models\Score;
use App\Models\Topic;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class AssignScoreToUser
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  LikeCreated  $event
     * @return void
     */
    public function handle(LikeCreated $event)
    {
        $like = $event->like;
        $target = $like->target;
        $targetType = $like->target_type;

        $info = ['original' => $like->toArray()];
        $info['action_id'] = $like->id;
        $info['action_title'] = '';
        $log = __METHOD__ . ", target_type: $targetType";
        switch ($targetType) {
            case Comment::class:
                if ($target->is_main_floor) {
                    $topic = $target->topic;
                    $info['target_id'] = $topic->id;
                    $info['target_title'] = $topic->titleExcerpt;
                    $info['target_username'] = $topic->user->username;
                } else {
                    $comment = $target;
                    $info['target_id'] = $comment->id;
                    $info['target_title'] = $comment->detail_excerpt;
                    $info['target_username'] = $comment->user->username;
                }
                break;
            default:
                break;

        }
        Score::assignForCreateLike($like->uid, null, null, $info);

        $log .= ", create by: {$like->uid}, info: " . json_encode($info, 336);
        logger($log);
    }
}
